/*
 * CFileUploadClient.h
 *
 *  Created on: 2017年11月8日
 *      Author: zhengboyuan
 */

#ifndef CFILEUPLOADCLIENT_H_
#define CFILEUPLOADCLIENT_H_

#include "FileUploadQueue.h"
#include "TCriticalSection.h"
#include "TThread.h"
#include "mongoose.h"
#include "MultipartStream.h"

class CFileUploadClient: public FileUploadClient, comn::Thread
{
public:
	static const int BUFFER_SIZE = 1024 * 64;

public:
	CFileUploadClient();
	virtual ~CFileUploadClient();

	virtual bool upload(const std::string& url, const StringMap& headers, const StringMap& formData, const FileUploadArray& files);

	virtual void close();

	virtual void cancel();

	virtual void setSink(Sink* sink);

	virtual const std::string& getUrl();

	virtual int getId();

	virtual void setId(int id);

	void handleEvent(struct mg_connection *nc, int ev, void *ev_data);

protected:
	virtual int run();
	virtual void doStop();

	void fireStatus(int status, const std::string& result);
	void fireProgress();

protected:
	FileUploadTask	m_task;
	Sink*	m_sink;

	struct mg_mgr	m_mgr;
	mg_connection*	m_connection;

	MultipartStream	m_stream;

	char	m_buffer[BUFFER_SIZE];


};



#endif /* CFILEUPLOADCLIENT_H_ */
